// carPackage/pages/feedBack/feedBack.js
import { feedBackAction } from "../../../service/config"
import { chooseMultipleImgUploadQiniuAction } from '../../../service/upload_service'

import debounce from "../../../tools/debounce"

let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    showSuccessPop: false,
    feedArrs: [
      {
        title: "数据缺失",
        value: 1
      },
      {
        title: "建议优化",
        value: 2
      },
      {
        title: "程序错误",
        value: 3
      },
      {
        title: "其它反馈",
        value: 4
      },

    ],
    feedIndex: 10,
    carDescNum: 0,
    formData: {
      content: "",
      type: 5,
      images: [],
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("JJLog:==:" + JSON.stringify(app.globalData.suggestData))
    this.setData({
      formData: app.globalData.suggestData
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },
  feedItemAction(e) {
    let item = e.currentTarget.dataset.item;
    let index = e.currentTarget.dataset.index;
    this.setData({
      feedIndex: index,
      'formData.type': item.title
    })

  },
  carDescInput(e) {
    this.setData({
      'formData.content': e.detail.value,
      carDescNum: e.detail.cursor
    })
  },
  //提交数据
  submitInfo() {
    if (this.data.formData.type == 5) {
      wx.showToast({
        title: '请选择反馈类型',
        icon: "none"
      })
      return;
    }
    if (!this.data.formData.content) {
      wx.showToast({
        title: '请输入您的意见或者建议',
        icon: "none"
      })
      return;
    }
    debounce(this.submitAction);

  },
  submitAction() {
    wx.showLoading();
    feedBackAction(this.data.formData).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        this.setData({
          showSuccessPop: true
        })
      }
    }).catch(err => {
      console.error("feedBackAction:", err);
      wx.hideLoading();
      wx.showToast({
        title: err || "提交失败",
      })
    })
  },

  successPopHide() {
    this.setData({
      showSuccessPop: false
    })
  },
  sureAction() {
    this.setData({
      showSuccessPop: false
    })
    wx.navigateBack({
      delta: 0,
    })
  },
  pushMultiImgs() {
    let arr = [];
    let images = this.data.formData.images || [];
    chooseMultipleImgUploadQiniuAction('car').then(res => {
      console.log("JJLog:图片上传成功:" + JSON.stringify(res));
      wx.showToast({
        title: '上传成功',
      })

      res.forEach((item) => {
        let obj = {
          fileType: 1,//图片
          url: ""
        }
        obj.url = item.complateUrl;
        arr.push(obj);
      })
      this.setData({
        'formData.images': images.concat(arr),
      })



    }).catch(err => {
      console.log("JJLog:图片上传失败:" + JSON.stringify(err));
      wx.showToast({
        title: '图片上传失败',
        icon: "none"
      })
    })
  },
  cancelPushImgAction(e) {
    let index = e.currentTarget.dataset.index;
    let arr = this.data.formData.images;
    arr.splice(index, 1);
    this.setData({
      'formData.images': arr
    })
  },


  previewImgs(e) {
    let url = e.currentTarget.dataset.url;
    let imgs = e.currentTarget.dataset.urls;
    let arr = [];
    imgs.forEach((item) => {
      arr.push(item.url)
    })
    wx.previewImage({
      current: url,
      urls: arr,
    })
  },



})